<?php
/**
 * Created by PhpStorm.
 * User: patrickwu
 * Date: 2020/6/16
 * Time: 10:27
 */
namespace App\Http\Controllers\Live;

use App\Service\Live\AlterService;
use App\Service\Live\ClientService;
use App\Service\Live\OperateService;
use App\Constant\RedisKeyConstant;
use Carbon\Carbon;
use Illuminate\Support\Facades\Redis;

class TalkController extends BaseController
{
    /**
     * client talk event handle
     * @param $websocket
     * @param $data
     */
    public function onTalk($websocket, $data)
    {
        $user_id    =   $websocket->getUserId();
        //check if user_id in block user set
        $client_service =   new ClientService();
        if (!$client_service->inBlock($this->current_room, $user_id)) {
            $user_info  =   $client_service->setUser($user_id);
            $receive['message'] =   $data['message'] ?? '';
            $receive['user_id'] =   $user_info['user_id'] ?? $user_id;
            $receive['nick_name']=  $user_info['nick_name'] ?? $user_id;
            $receive['socket_id']=  $user_id;
            echo $user_id . ' is talking...' . PHP_EOL;

            //broadcast to everyone include sender
            $websocket->to($this->current_room)->emit('clientReceiveMessage', $receive);
        }
    }

    /**
     * client click goods event handle
     * @param $websocket
     * @param $data
     */
    public function onClickGoods($websocket, $data)
    {
        $service    =   new OperateService();
        if (empty($data['good_id'])) {
            return;
        }
        $client_service =   new ClientService();
        $user_id    =   $websocket->getUserId();
        $user_info  =   $client_service->setUser($user_id);

        $service->clickGoods($this->current_room, $data['good_id']);
        $clickGoodsNum  =   $service->getClickGoods($this->current_room);
        $websocket->to($this->current_room)->emit('clientClickGoods', ['user_id' => $user_id, 'nick_name' =>  $user_info['nick_name'] ?? '', 'client_goods' => $clickGoodsNum]);
    }

    /**
     * client click thumb-up handle
     * @param $websocket
     * @param $data
     */
    public function onThumbUp($websocket, $data)
    {
        $service    =   new AlterService();
        $user_id    =   $websocket->getUserId();
        $service->userThumb($this->current_room, $user_id);

        $total_thumb=   $service->getThumb($this->current_room);
        $websocket->to($this->current_room)->emit('clientTotalThumb', $total_thumb);
    }

    /**
     * client user fetch coupon
     * @param $websocket
     * @param $data
     */
    public function onFetchCoupon($websocket, $data)
    {
        $service    =   new AlterService();
        $user_id    =   $websocket->getUserId();
        [$flag, $result]     =   $service->userFetch($user_id, $data['coupon_id'] ?? '', $this->current_room);
        if ($flag) {
            $emit['code']    =   0;
            $emit['message'] =   '';
            /**
             * emit backend
             */
            $new_info     =   (new OperateService())->couponsInfoSort($this->current_room);
            $emit_msg     =   ['coupons' => $new_info, 'msg' => '领取优惠券成功'];
            $websocket->to($this->current_room)->emit('backendCoupons', $emit_msg);
        } else {
            $emit['code']    =    -1;
            $emit['message'] =    $result;
        }

        /**
         * emit client event
         */
        $websocket->to($user_id)->emit('clientFetchResult', $emit);
    }

    /**
     * user share room
     * @param $websocket
     * @param $data
     */
    public function onShare($websocket, $data)
    {
        $service    =   new AlterService();
        $user_id    =   $websocket->getUserId();
        $service->userShare($this->current_room, $user_id);

        $user_info  =   (new ClientService())->setUser($user_id);
        $websocket->to($this->current_room)->emit('clientReceiveShare', [
            'user_id'   =>  $user_id,
            'nick_name' =>  $user_info['nick_name'] ?? ''
        ]);
    }
}
